package com.woniuxy.juc.reentrant;

import com.woniuxy.untis.Sleep;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:  LockSupport 实现循环打印
 * <P> Date: 2022/4/8:8:48
 * <P> Description:
 */
@Slf4j(topic = "c.SupportLoopPrint")
public class SupportLoopPrint {
    static Thread t1;
    static Thread t2;
    static Thread t3;

    public static void main(String[] args) {
        ParkUnpark parkUnpark = new ParkUnpark(5);
        t1 = new Thread(() -> {
            parkUnpark.print("a", t2);
        }, "t1");



        t2 = new Thread(() -> {
            parkUnpark.print("b", t3);
        }, "t2");


        t3 = new Thread(() -> {
            parkUnpark.print("c",t1);
        }, "t3");
        t1.start();
        t2.start();
        t3.start();

        Sleep.sleep(1000);
        LockSupport.unpark(t1);
    }

}

@Data
@AllArgsConstructor
@NoArgsConstructor
class ParkUnpark {
    private int loopNumber;

    public void print(String str, Thread next) {
        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.println(str);
            LockSupport.unpark(next);
        }
    }
}
